

% plot temporal distribution

displayWindow    = [-0.1 0.5];
amplitudeLimits  = [-2e-13 2e-13];

cfg              = [];
cfg.xlim         = displayWindow;
cfg.ylim         = amplitudeLimits;
cfg.showlabels   = 'no';
cfg.linewidth    = 2;
cfg.graphcolor   = 'brgk';
cfg.baseline     = [-0.5 0];
cfg.baselinetype = 'absolute';

subplot(4,2,[1,4])

cfg.channel      = {'MZP'};
ft_singleplotER(cfg, dataFaces, dataScenes, dataBodies,dataObjects);

legend({'Faces','Scenes'},'Location','North');
legend boxoff


% plot spatial distribution

cfg                 = [];
cfg.layout          = 'CTF275.lay';
cfg.xlim            = [0.05 0.15];
cfg.zlim            = amplitudeLimits;
cfg.baseline        = [-0.5 0];
cfg.baselinetype    = 'absolute';
cfg.comment         = 'no';
cfg.colorbar        = 'yes';
cfg.interpolation   = 'v4';
cfg.interplimits    = 'head';
cfg.style           = 'both';
cfg.shading         = 'interp';
cfg.marker          = 'on';

subplot(4,2,5)
ft_topoplotER(cfg, dataFaces);
title('Faces')

subplot(4,2,6)
ft_topoplotER(cfg, dataScenes);
title('Scenes')

subplot(4,2,7)
ft_topoplotER(cfg, dataBodies);
title('Bodies')

subplot(4,2,8)
ft_topoplotER(cfg, dataObjects);
title('Objects')


% plot spatiotemporal distribution

cfg                 = [];
cfg.layout          = 'CTF275.lay';
cfg.xlim            = 0 : 0.1 : 0.4;
cfg.zlim            = amplitudeLimits;
cfg.baseline        = [-0.5 0];
cfg.baselinetype    = 'absolute';
cfg.comment         = 'xlim';
cfg.colorbar        = 'yes';
cfg.interpolation   = 'v4';
cfg.interplimits    = 'head';
cfg.style           = 'both';
cfg.shading         = 'interp';
cfg.marker          = 'on';

figure
ft_topoplotER(cfg, dataFaces);
title('Faces')

figure
ft_topoplotER(cfg, dataScenes);aviObjectaviObject = close(aviObject); = close(aviObject);
title('Scenes')

figure
ft_topoplotER(cfg, dataBodies);aviObject = close(aviObject);
title('Bodies')

figure
ft_topoplotER(cfg, dataObjects);aviObject = close(aviObject);
title('Objects')aviObject = close(aviObject);



% make movie of spatiotemporal dynamcis

outputdir   = '~/data/alebac_decperc/output/';

slidingWindowSize   = .02;
startTimePoints     = 0:0.005:0.5-slidingWindowSize;
endTimePoints       = startTimePoints + slidingWindowSize;
timingParameters    = [startTimePoints' endTimePoints'];

cfgTopo                 = [];
cfgTopo.layout          = 'CTF275.lay';
cfgTopo.zlim            = amplitudeLimits;
cfgTopo.baseline        = [-0.4 0];
cfgTopo.baselinetype    = 'absolute';
cfgTopo.comment         = 'xlim';
cfgTopo.colorbar        = 'no';
cfgTopo.interpolation   = 'v4';
cfgTopo.interplimits    = 'head';
cfgTopo.style           = 'straight';
cfgTopo.shading         = 'interp';
cfgTopo.marker          = 'off';

cfgSingle               = [];
cfgSingle.zlim          = amplitudeLimits;
cfgSingle.baseline      = [-0.4 0];
cfgSingle.baselinetype  = 'absolute';
cfgSingle.linewidth     = 2;
cfgSingle.graphcolor    = 'brgc';
cfgSingle.xlim          = [0 0.5];

erfFigure           = figure('Position',[100 100 900 1300]);
windowSize          = get(erfFigure,'Position');
windowSize(1:2)     = [0 0];
nFrames             = size(timingParameters,1);
frameCaptureData    = moviein(nFrames,erfFigure,windowSize);

set(erfFigure,'NextPlot','replacechildren')

aviObject           = avifile([outputdir 'test.avi'],'compression','none');

for i = 1:nFrames
    
    cfgTopo.xlim        = [timingParameters(i,1) timingParameters(i,2)];
    currentTimePoint    = timingParameters(i,1)+(slidingWindowSize/2);
    
    subplot(4,4,1)
    ft_topoplotER(cfgTopo,dataFaces);
    title('Faces')
    
    subplot(4,4,2)
    ft_topoplotER(cfgTopo,dataScenes);
    title('Scenes')
    
    subplot(4,4,3)
    ft_topoplotER(cfgTopo,dataBodies);
    title('Bodies')
    
    subplot(4,4,4)
    ft_topoplotER(cfgTopo,dataObjects);
    title('Objects')
    
    subplot(4,4,[5 8])
    cfgSingle.channel       = {'MZP'};
    ft_singleplotER(cfgSingle,dataFaces,dataScenes,dataBodies,dataObjects);
    legend({'Faces','Scenes','Bodies','Objects'},'Location','North');
    legend boxoff
    title('Midline parietal')
    line([currentTimePoint currentTimePoint],[amplitudeLimits(1) amplitudeLimits(2)],'Marker','.','LineStyle','-','Color','k','LineWidth',3)
    
    subplot(4,4,[9 12])
    cfgSingle.channel       = {'MLP','MLO'};
    ft_singleplotER(cfgSingle,dataFaces,dataScenes,dataBodies,dataObjects);
    legend({'Faces','Scenes','Bodies','Objects'},'Location','North');
    legend boxoff
    title('Left posterior')
    line([currentTimePoint currentTimePoint],[amplitudeLimits(1) amplitudeLimits(2)],'Marker','.','LineStyle','-','Color','k','LineWidth',3)
    
    subplot(4,4,[13 16])
    cfgSingle.channel       = {'MRP','MRO'};
    ft_singleplotER(cfgSingle,dataFaces,dataScenes,dataBodies,dataObjects);
    legend({'Faces','Scenes','Bodies','Objects'},'Location','North');
    legend boxoff
    title('Right posterior')
    line([currentTimePoint currentTimePoint],[amplitudeLimits(1) amplitudeLimits(2)],'Marker','.','LineStyle','-','Color','k','LineWidth',3)
    
    frameCaptureData (:,i) = getframe(erfFigure,windowSize);
    
    aviObject = addframe(aviObject,frameCaptureData (:,i));
end

aviObject = close(aviObject);



% experimental contrasts

contrast         = dataScenes;
contrast.avg     = dataScenes.avg - dataObjects.avg;

contrast.avg     = dataFaces.avg - dataScenes.avg;

amplitudeLimits  = [-1.5e-13 1.5e-13];
cfg.zlim         = amplitudeLimits;

figure
ft_topoplotER(cfg, contrast);
title('Faces - Scenes')



